import 'package:flutter/material.dart';
import 'package:getx_scaffold/getx_scaffold.dart';
import 'package:oramemo/common/styles/colors.dart';
import 'package:oramemo/pages/circle/invcode/invcode_logic.dart';
import 'package:oramemo/pages/circle/invcode/invcode_state.dart';
import 'package:qr_flutter/qr_flutter.dart';

class ErcodeView extends StatelessWidget {
  const ErcodeView({super.key});

  @override
  Widget build(BuildContext context) {
    final InvcodeLogic logic = Get.put(InvcodeLogic());
    final InvcodeState state = Get
        .find<InvcodeLogic>()
        .state;
    return Column(
      children: [
        RepaintBoundary(
          key: logic.qrKey,
          child: Column(
            children: [
              const SizedBox(height: 20),
              Center(
                child: Container(
                  width: 177.w,
                  height: 156.h,
                  color: const Color(0xFFEEEEEF),
                  child: Obx(() {
                    return Center(
                      child: QrImageView(
                        data: state.inviteCode.value,
                        version: QrVersions.auto,
                        size: 156.w,
                        backgroundColor: const Color(0xFFEEEEEF),
                      ),
                    );
                  }),
                ),
              ),
              SizedBox(height: 18.h),

              // 提示文字
              Center(
                child: Text(
                  '让家人亲友扫描上面的二维码，加入设备圈子',
                  style: TextStyle(
                    fontSize: 12.sp,
                    color: Color(0xFF888992),
                  ),
                ),
              ),

              SizedBox(height: 27.h),

            ],
          ),
        ),
        // 底部按钮
        Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Obx(() => TextButton(
              onPressed: state.isSaving.value ? null : logic.saveToGallery,
              child: state.isSaving.value
                  ? SizedBox(
                width: 16.w,
                height: 16.h,
                child: const CircularProgressIndicator(
                  strokeWidth: 2,
                  valueColor: AlwaysStoppedAnimation<Color>(Color(0xFF2573FF)),
                ),
              )
                  : Text(
                '保存到相册',
                style: TextStyle(
                  color: const Color(0xFF2573FF),
                  fontSize: 14.sp,
                  fontFamily: 'Inter',
                  fontWeight: FontWeight.w400,
                ),
              ),
            )),
            SizedBox(width: 16.w),
            // 替换水平Divider为垂直分割线
            Container(
              height: 29.h,
              width: 1,
              color: const Color(0xFFEEEEEF),
            ),
            SizedBox(width: 16.w),
            Obx(() => TextButton(
              onPressed: state.isSharing.value ? null : logic.shareQrCode,
              child: state.isSharing.value
                  ? SizedBox(
                width: 16.w,
                height: 16.h,
                child: const CircularProgressIndicator(
                  strokeWidth: 2,
                  valueColor: AlwaysStoppedAnimation<Color>(Color(0xFF2573FF)),
                ),
              )
                  : Text(
                '分享二维码',
                style: TextStyle(
                  color: const Color(0xFF2573FF),
                  fontSize: 14.sp,
                  fontFamily: 'Inter',
                  fontWeight: FontWeight.w400,
                ),
              ),
            )),
          ],
        ),
        SizedBox(height: 8.h),
      ],
    );
  }
}
